<?php

/**
 * 基类通用控制器
 */
class BaseController extends \Yaf\Controller_Abstract
{

    public $user_info;
    public $layout = 'layout/BaseLayout.phtml';

    public function init()
    {

        $config = \Yaf\Application::app()->getConfig()->app;
        if ($config['is_session_auth'] == 1) {
            $this->user_info = \Yaf\Session::getInstance()->get("user_info");
        }
        //use layout
        if ($this->layout) {
            $this->setLayout($this->layout);
        }
        $this->getView()->user_info = $this->user_info;
    }

    //是否ajax请求
    public function isAjax()
    {
        if (isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") {
            return true;
        }
        return false;
    }

    /**
     * API接口成功返回数据结构
     */
    public function successJson($errCode = 0, $errMsg = "", $data = [])
    {
        $ua_config = \Yaf\Application::app()->getConfig()->host;
        header('Content-type:application/json;charset=utf8');
        header('Access-Control-Allow-Origin:' . $ua_config['url']);
        $res = ['errCode' => $errCode, 'errMsg' => $errMsg, 'data' => $data];
        exit(json_encode($res));
    }

    /**
     * API接口失败返回数据结构
     */
    public function errorJson($errCode = 1, $errMsg = "", $data = [])
    {
        $ua_config = \Yaf\Application::app()->getConfig()->app;
        header('Content-type:application/json;charset=utf8');
        header('Access-Control-Allow-Origin:' . $ua_config['url']);
        $res = ['errCode' => $errCode, 'errMsg' => $errMsg, 'data' => $data];
        exit(json_encode($res));
    }

    //dataTable数据返回格式
    public function dataTable($data, $count)
    {
        header('Content-type:application/json;charset=utf8');
        $res = array(
            'sEcho' => $_POST['draw'],
            'iTotalDisplayRecords' => $count,
            'iTotalRecords' => $count,
            'aaData' => $data,
        );
        exit(json_encode($res));
    }

    /**
     * @param null $name
     * @return mixed|null
     */
    public function get($name = null, $default = "", $isXss = true)
    {
        //静态路由没有$_GET
        if (!$_GET) {
            $_GET = $this->getRequest()->getParams();
        }

        if (!$name) {
            $data = $_GET;
        } elseif (isset($_GET[$name])) {
            $data = $_GET[$name];
        } else {
            return $default;
        }

        if ($isXss)
            return $this->xssClean($data);
        return $data;
    }

    /**
     * @param null $name
     * @return mixed|null
     */
    public function post($name = null, $default = "", $isXss = true)
    {
        if (!$name) {
            $data = $_POST;
        } elseif (isset($_POST[$name])) {
            $data = $_POST[$name];
        } else {
            return $default;
        }

        if ($isXss)
            return $this->xssClean($data);
        return $data;
    }

    /**
     * @param $data
     * @return mixed
     */
    public function xssClean($data)
    {
        if (is_array($data)) {
            return filter_var_array($data, FILTER_SANITIZE_STRING);
        } else {
            return filter_var($data, FILTER_SANITIZE_STRING);
        }
    }

    /**
     * 设置布局
     * @param string $layout 布局文件
     */
    public function setLayout($layout)
    {
        $this->getResPonse()->layout = function ($content) use ($layout) {
            //$content子视图的内容
            return $this->getView()->render($layout, ['content' => $content]);
        };
    }
}
